import Vue from 'vue'
import VueRouter from 'vue-router'
import ShouYeComponent from '../views/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'ShouYe',
    component:ShouYeComponent,
    children: [
      {
        path: '/newsItem',
        component: ()=>import("@/components/BlogItems")
      }]
  },
  {
    path: '/newsDetail/:id',
    name: 'newsDetail',
    component: ()=>import("@/views/newsDetail")
  },
  {
    path:'/login',
    name:"login",
    component:() => import('../views/LoginView.vue')
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
  },
  {
    path: '/searchPage',
    name: 'searchPage',
    component: () => import(/* webpackChunkName: "about" */ '../views/SearchBlogs.vue')
  },
  {
    path:"/admin",
    name:'admin',
    component:()=>import("../views/AdminHome.vue"),
    children:[
      {
        path: '/category',
        name:"category",
        component:()=>import("../views/Category.vue"),
      },
      {
        path: "/user",
        name:"user",
        component:()=>import("@/views/UserView")
      },
      {
        path:'/comment',
        name:'comment',
        component:()=>import('@/views/AdminComment.vue')
      },
      {
        path:"index",
        name:"index",
        component:()=>import("../views/AdminIndex.vue")
      },
      {
        path:"blog",
        name:"blog",
        component:()=>import("../views/AdminBlog.vue")
      },
      {
        path: 'tag',
        name:"tag",
        component:()=>import("../views/TagView.vue"),
      },
      {
        path: '/link',
        name:'link',
        component:()=>import("../views/AdminLink")
      },
        //博客管理
      {
        path: '/allMessages',
        component: ()=>import("@/views/allMassage.vue"),
        meta: {
          requiresAuth: true
        },
        children: [
          {
            path: '/addText',
            component:()=>import("@/views/addNews.vue"),
            meta: {
              requiresAuth: true
            },
          },
          {
            path: '/updateText',
            component: ()=>import("@/views/UpdateMassage.vue"),
            meta: {
              requiresAuth: true
            },
          }
          ]
      },
      // {
      //   path: '/manu',
      //   component:()=>import("@/views/manu.vue"),
      //   children: [
      //     // 分类管理
      //     {
      //       path: '/category',
      //       name:"category",
      //       component:()=>import("../views/Category.vue"),
      //     },
      //     {
      //       path: '/allMessages',
      //       component: ()=>import("@/views/allMassage.vue"),
      //       meta: {
      //         requiresAuth: true
      //       },
      //       children: [
      //         {
      //           path: '/addText',
      //           component:()=>import("@/views/addNews.vue"),
      //           meta: {
      //             requiresAuth: true
      //           },
      //         },
      //         {
      //           path: '/updateText',
      //           component: ()=>import("@/views/UpdateMassage.vue"),
      //           meta: {
      //             requiresAuth: true
      //           },
      //         },
      //       ]
      //     }
      //   ]
      // },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path.includes("/admin/index")){
    let loginuser  = localStorage.getItem("loginUser");
    if(loginuser){
      //有，则正常跳转
      next();//放行
    }else{
      //没有，则重定向到登录页面
      next("/login")
    }
  }else{
    //去的不是index，直接放行
    next();//用于放行表示正常跳转
  }
})
export default router
